﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAnalyzer
{
    class Program
    {
        static string folderPath = @"C:\Users\cemhe_000\Dropbox\master\Difüzyon\3dDiffision\3dDiffisionWithDecomposition\BinomialSimulatorSimulator\bin\Release\Logs\noDec\d = 79";

        static void Main(string[] args)
        {
            for (long i = 4000000; i <= 4096000000; i *= 2)
            {
                if (!File.Exists(folderPath + "\\" + string.Format("Histogram D=14, N=60000, Dec=True, HL={0}.txt", i)))
                {
                    continue;
                }
                AnalyzeHistogramFile(folderPath + "\\" + string.Format("Histogram D=14, N=60000, Dec=True, HL={0}.txt", i));
            }
            try 
	        {
                AnalyzeHistogramFile(folderPath + "\\" + string.Format("Histogram D=14, N=6000, Dec=False, HL=4096000000.txt"));
		
	        }
	        catch (Exception e)
	        {
                Console.WriteLine(e.Message);
	        }
            
        }

        static void AnalyzeHistogramFile(string fileName)
        {
            var streamWriter = new StreamWriter(fileName + "_percentales.txt");

            var dataList = new List<double>();

            var allLines = File.ReadAllLines(fileName);

            for (int i = 0; i < allLines.Length; i++)
            {
                dataList.Add(double.Parse(allLines[i]));
            }

            double totalHits = dataList.Sum();

            double currentSum = 0;
            var sixtyPercentCutOffIndex = -1;
            var listOfPercentages = new List<double>();

            var flag = true;

            for (int i = 0; i < dataList.Count; i++)
            {
                currentSum += dataList[i];

                if (currentSum > (totalHits * 0.59) && flag)
                {
                    sixtyPercentCutOffIndex = i;
                    flag = false;
                }

                listOfPercentages.Add(currentSum / totalHits);
            }

            listOfPercentages.ForEach((x) => { streamWriter.WriteLine(x); });

            streamWriter.WriteLine("60 % cutoff time = " + sixtyPercentCutOffIndex / 100.0);

            var meanTimeOfArrival = 0.0;

            for (int i = 0; i < dataList.Count; i++)
            {
                meanTimeOfArrival += dataList[i] * i;
            }

            meanTimeOfArrival /= (100 * dataList.Count);

            streamWriter.WriteLine("Mean arrival time = " + meanTimeOfArrival);

            var sumStdDev = 0.0;

            for (int i = 0; i < dataList.Count; i++)
            {
                var d = ((i / 100.0 - meanTimeOfArrival )) * dataList[i];

                d = d * d;

                sumStdDev += d;
            }

            var standardDev = Math.Sqrt(sumStdDev / totalHits);

            streamWriter.WriteLine("stdDev arrival time = " + standardDev);

            streamWriter.Flush();
            streamWriter.Close();
        }
    }
}
